package com.uyayo.system.monitor.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uyayo.framework.message.model.Result;
import com.uyayo.framework.pagehelper.pojo.OrderBy;
import com.uyayo.framework.pagehelper.pojo.PageBean;
import com.uyayo.framework.pagehelper.util.OrderByUtil;
import com.uyayo.system.monitor.entity.SysException;
import com.uyayo.system.monitor.mapper.SysExceptionMapper;
import com.uyayo.system.monitor.model.SysExceptionCondition;
import com.uyayo.system.monitor.service.ExceptionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.ArrayList;
import java.util.List;


@Service("exceptionsService")
public class ExceptionsServiceImpl implements ExceptionsService {

	private final SysExceptionMapper sysExceptionMapper;

	@Autowired
	public ExceptionsServiceImpl(SysExceptionMapper sysExceptionMapper) {
		this.sysExceptionMapper = sysExceptionMapper;
	}

	@Override
	public PageBean<SysException> selectException(int pageCurrent, Integer pageSize, String sort, String order,
			SysExceptionCondition sysExceptionCondition){
		List<SysException> exception;
		//分页处理
		List<OrderBy> orderBies = new ArrayList<>();
		orderBies.add(new OrderBy("startTime", "start_time"));
		orderBies.add(new OrderBy("endTime", "end_time"));
		orderBies.add(new OrderBy("created", "created"));

		OrderByUtil orderByUtil = new OrderByUtil();
		String orderBy = orderByUtil.getOrderBy(orderBies, sort, order);
		PageHelper.startPage(pageCurrent, pageSize);
		
		Example example = new Example(SysException.class);
		if(!StringUtils.isEmpty(orderBy)){
			example.setOrderByClause(orderBy);
		}
		Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(sysExceptionCondition.getClazz())){
			criteria.andLike("clazz","%"+sysExceptionCondition.getClazz()+"%");
		}
		if(!StringUtils.isEmpty(sysExceptionCondition.getMethodName())){
			criteria.andLike("methodName","%"+sysExceptionCondition.getMethodName()+"%");
		}
		if(!StringUtils.isEmpty(sysExceptionCondition.getDescription())){
			criteria.andLike("description","%"+sysExceptionCondition.getDescription()+"%");
		}

		if(sysExceptionCondition.getBeginCreatedTime()!=null || sysExceptionCondition.getEndCreatedTime()!=null){
			criteria.andBetween("createdTime", sysExceptionCondition.getBeginCreatedTime(), sysExceptionCondition.getEndCreatedTime());
		}

		exception = sysExceptionMapper.selectByExample(example);
		//取记录总条数
		PageInfo<SysException> pageInfo = new PageInfo<>(exception);
			
		return new PageBean<>(pageInfo.getTotal(), pageCurrent, pageSize, exception);
}

	@Override
	public int saveSysException(SysException sysException) {
		return sysExceptionMapper.insertSelective(sysException);
	}

	
	@Override
	public Result deleteException(String ids)throws Exception {
		Result customResult;
		int countDelete = 0;
		int countDelete_ = 0;
		if (!StringUtils.isEmpty(ids)) {
			String[] idArr = ids.split(",");
			for (String anIdArr : idArr) {
				if (!anIdArr.equals("")) {
					sysExceptionMapper.deleteByPrimaryKey(anIdArr);
					countDelete++;
				} else {
					countDelete_++;
				}
			}
			customResult = Result.build(200,"成功删除"+countDelete+"条记录，有"+countDelete_+"条记录删除失败！");
		}else{
			customResult = Result.build(400,"请选择要删除的监听事件记录！");
		}
		return customResult;
	}


	@Override
	public SysException getExceptionsById(String id) throws Exception {
		return sysExceptionMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<SysException> selectException(String sort, String order, SysExceptionCondition sysExceptionCondition) {

		//分页处理
		List<OrderBy> orderBies = new ArrayList<>();
		orderBies.add(new OrderBy("startTime", "start_time"));
		orderBies.add(new OrderBy("endTime", "end_time"));
		orderBies.add(new OrderBy("created", "created"));

		OrderByUtil orderByUtil = new OrderByUtil();
		String orderBy = orderByUtil.getOrderBy(orderBies, sort, order);

		Example example = new Example(SysException.class);
		if(!StringUtils.isEmpty(orderBy)){
			example.setOrderByClause(orderBy);
		}
		Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(sysExceptionCondition.getClazz())){
			criteria.andLike("clazz","%"+sysExceptionCondition.getClazz()+"%");
		}
		if(!StringUtils.isEmpty(sysExceptionCondition.getMethodName())){
			criteria.andLike("methodName","%"+sysExceptionCondition.getMethodName()+"%");
		}
		if(!StringUtils.isEmpty(sysExceptionCondition.getDescription())){
			criteria.andLike("description","%"+sysExceptionCondition.getDescription()+"%");
		}

		if(sysExceptionCondition.getBeginCreatedTime()!=null || sysExceptionCondition.getEndCreatedTime()!=null){
			criteria.andBetween("createdTime", sysExceptionCondition.getBeginCreatedTime(), sysExceptionCondition.getEndCreatedTime());
		}

		return sysExceptionMapper.selectByExample(example);
	}


}
